<?php

class Companies extends Database
{
	protected $id;
	protected $companyname;
	
	function Companies($id, $companyname)
	{
        parent::__construct();
		$this->id=$id;		
		$this->companyname=$companyname;
	}
	
	function printCompaniesAdmin($res)
	{
		print("
		<p align=center>
		<table align=center border=1 cellpadding=5 cellspacing=0>
		 <tr bgcolor=#EEEEEE>
		  <td width=500><b>Назва компанії</b></td>
		  <td width=100><b>Редагування</b></td>	  
		 </tr>
		");	
		while ($row = mysql_fetch_array($res))
		{
			print("<tr>");
			print("
			 <td width=500>
			  ".$row[1]."
			 </td>
			 <td width=100>
			  <a href=/index.php?page=company_edit&company=".$row[0].">Редагувати</a>
			 </td>
			 ");
			print("</tr>");
		}	
		print("</table>");
		print("<p align=center><br>");				
	}

    function printCompanyName()
    {
        $sql=mysql_query("SELECT companyname FROM companies WHERE id='$this->id'");
        $sql=mysql_fetch_array($sql);
        return $sql[0];
    }

    function existingAdd()
    {
        $name=mysql_real_escape_string($this->companyname);
        $sql=mysql_query("SELECT COUNT(*) FROM companies WHERE companyname= '$name'");
        $sql=mysql_fetch_array($sql);
        if($sql[0]!=0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    function existingEdit()
    {
        $name=mysql_real_escape_string($this->companyname);
        $sql=mysql_query("SELECT COUNT(*) FROM companies WHERE companyname= '$name' AND id!='$this->id'");
        $sql=mysql_fetch_array($sql);
        if($sql[0]!=0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

	function add()
	{
		mysql_query("INSERT INTO companies (`companyname`) VALUES ('$this->companyname')");
	}
	
	function edit()
	{
		mysql_query("UPDATE companies SET `companyname`='$this->companyname' WHERE id='$this->id'");
	}
	
	function show()
	{		
		$res = mysql_query("SELECT id, companyname FROM companies ORDER BY companyname");
		if ($_SESSION['login']=="admin") 
		{
			$this->printCompaniesAdmin($res);
		}
	}

}

?>